<?php

/**
 * This is the model class for table "pmi_wiki".
 *
 * The followings are the available columns in table 'pmi_wiki':
 * @property string $id
 * @property string $category
 * @property string $syntax
 * @property string $content
 * @property string $create_time
 * @property string $update_time
 * @property string $title
 * @property string $keyword
 */
class Wiki extends pmiActiveRecord
{
	private $_oldKeyword;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Wiki the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{wiki}}';
	}
	
	/*return syntax list*/
	public function getSyntaxList(){
		return array('plain'=>'plain','text'=>'text','php'=>'php','html'=>'html','css'=>'css','js'=>'js','sql'=>'sql','coldfusion'=>'coldfusion','as3'=>'as3','bash'=>'bash','shell'=>'shell','csharp'=>'csharp','cpp'=>'cpp','c'=>'c','delphi'=>'delphi','java'=>'java','javafx'=>'javafx','perl'=>'perl','pl'=>'pl','powershell'=>'powershell','python'=>'python','ruby'=>'ruby','vb'=>'vb','vbnet'=>'vbnet','xml'=>'xml','xslt'=>'xslt');
	}
	
	/**
	 * This is invoked when a record is populated with data from a find() call.
	 */
	protected function afterFind()
	{
		parent::afterFind();
		$this->_oldKeyword=$this->keyword;
	}

	/**
	 * This is invoked after the record is saved.
	 */
	protected function afterSave()
	{
		parent::afterSave();
		Tag::model()->updateFrequency($this->_oldKeyword, $this->keyword);
	}
	
	/**
	 * This is invoked after the record is deleted.
	 */
	protected function afterDelete()
	{
		parent::afterDelete();
		// Comment::model()->deleteAll('post_id='.$this->id);
		Tag::model()->updateFrequency($this->keyword, '');
	}
	
	/**
	 * @return string the URL that shows the detail of the post
	 */
	public function getUrl()
	{
		return Yii::app()->createUrl('wiki/view', array(
			'id'=>$this->id,
			'title'=>str_replace(' ','-',$this->title),
		));
	}

	/**
	 * @return array a list of links that point to the post list filtered by every tag of this post
	 */
	public function getTagLinks()
	{
		$links=array();
		foreach(Tag::string2array($this->keyword) as $tag)
			$links[]=CHtml::link(CHtml::encode($tag), array('wiki/index', 'tag'=>$tag));
		return $links;
	}
	
	/**
	 * Normalizes the user-entered tags.
	 */
	public function normalizeTags($attribute,$params)
	{
		$this->tags=Tag::array2string(array_unique(Tag::string2array($this->tags)));
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('category, title, keyword', 'length', 'max'=>255),
			array('content, create_time, update_time', 'safe'),
			array('syntax', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, category, syntax, content, create_time, update_time, title, keyword', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'category' => 'Category',
			'syntax' => 'Syntax',
			'content' => 'Content',
			'create_time' => 'Create Time',
			'update_time' => 'Update Time',
			'title' => 'Title',
			'keyword' => 'Tags',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('category',$this->category,true);
		$criteria->compare('syntax',$this->syntax,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('keyword',$this->keyword,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}